package java44th.group.service.wx.collect;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java44th.group.bean.common.CommonData;
import java44th.group.bean.po.*;
import java44th.group.bean.wx.bo.collect.ListBo;
import java44th.group.bean.wx.vo.collect.ListVo;
import java44th.group.mapper.MarketCollectMapper;
import java44th.group.mapper.MarketGoodsMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Aurhor: 徐云飞
 * @DATE: 2022/10/14 10:26
 * @Description:
 * @Version 1.0
 */
@Service
@Transactional
public class WxCollectServiceImpl implements WxCollectService {

    @Autowired
    MarketCollectMapper collectMapper;
    @Autowired
    MarketGoodsMapper goodsMapper;

    //藏品列表
    @Override
    public CommonData list(ListBo listBo) {
        PageHelper.startPage(listBo.getPage(), listBo.getLimit());

        Subject subject = SecurityUtils.getSubject();
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer id = primaryPrincipal.getId();
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        marketCollectExample.createCriteria().andUserIdEqualTo(id);
        List<MarketCollect> marketCollects = collectMapper.selectByExample(marketCollectExample);

        PageInfo<MarketCollect> marketCollectPageInfo = new PageInfo<>(marketCollects);

        CommonData<MarketCollect> data = CommonData.data(marketCollectPageInfo);


        ArrayList<MarketGoods> marketGoodss = new ArrayList<>();
        for (MarketCollect marketCollect : marketCollects) {

            if (listBo.getType() == 0) {
                MarketGoods marketGoods = goodsMapper.selectByPrimaryKey(marketCollect.getValueId());
                if (marketGoods != null) {
                    marketGoodss.add(marketGoods);
                }
            }
        }

        List<ListVo.ListDTO> listDTOS = new ArrayList<>();
        for (MarketGoods goodss : marketGoodss) {

            ListVo.ListDTO listDTO = new ListVo.ListDTO(goodss.getBrief(), goodss.getPicUrl(), goodss.getId(), goodss.getName()
                    , goodss.getId(), goodss.getBrandId(), goodss.getRetailPrice().intValue());

            listDTOS.add(listDTO);
        }


        return new CommonData<ListVo.ListDTO>(data.getTotal(), data.getPages()
                , data.getLimit(), data.getPage(), listDTOS);


    }

    @Override
    public void addordelete(Map map) {

        Subject subject = SecurityUtils.getSubject();
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer id = primaryPrincipal.getId();
        if ((int) map.get("type") == 0) {
            MarketCollectExample marketCollectExample = new MarketCollectExample();
            MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
            criteria.andUserIdEqualTo(id);
            criteria.andValueIdEqualTo((int) map.get("valueId"));
            List<MarketCollect> marketCollects = collectMapper.selectByExample(marketCollectExample);
            if (marketCollects.size() == 0) {

                MarketCollect marketCollect = new MarketCollect();
                marketCollect.setValueId((int) map.get("valueId"));
                marketCollect.setAddTime(new Date());
                marketCollect.setUpdateTime(new Date());
                marketCollect.setUserId(id);
                collectMapper.insertSelective(marketCollect);

            } else {

                collectMapper.deleteByExample(marketCollectExample);

            }
        }


    }
}
